<?php defined('SYSPATH') or die('No direct script access.');

class Model_Season
{  
    const SUMMER_SEASON   = 1;
    const DEMI_SEASON     = 2;
    const MULTI_SEASON    = 3;
    const WINTER_SEASON   = 4;
    
    public static $seasons = array(
        'leto'              => self::SUMMER_SEASON,
        'demisezon'         => self::DEMI_SEASON,
        'kruglogodichnyiy'  => self::MULTI_SEASON,
        'zima'              => self::WINTER_SEASON,
    );
    
    public static $season_options = array(       
        self::SUMMER_SEASON => array(
            'name'        => 'лето',
            'single_form' => 'на лето',
            'plural_form' => 'летние',
        ),
        self::DEMI_SEASON => array(
            'name'        => 'демисезон',
            'single_form' => 'демисезон',
            'plural_form' => 'демисезонные',
        ),
        self::MULTI_SEASON => array(
            'name'        => 'круглогодичный',
            'single_form' => 'мультисезон',
            'plural_form' => 'круглогодичные',
        ),
        self::WINTER_SEASON => array(
            'name'        => 'зима',
            'single_form' => 'на зиму',
            'plural_form' => 'зимние',
        ),
    );
    
    protected static function _compare($a, $b)
    {
        return $a['count'] < $b['count'];
    } 
    
    public static function get_seasons(Model_Category $category, $brand = null, $color = null, $composition = null, $shop = null, $price = null)
    {
        $ids = array(); 
        if ($category->childs) {
            $ids = explode(',', $category->childs);    
        }
        $ids[] = $category->id;
        
        $brand_id = $brand;
        if (is_array($brand)) {
            $brand_id = $brand['id'];   
        }
        
        $color_id = $color;
        if (is_array($color)) {
            $color_id = $color['id'];    
        }
        
        $composition_id = $composition;
        if (is_array($composition)) {
            $composition_id = $composition['id'];    
        }
        
        $shop_id = $shop;
        if (is_array($shop)) {
            $shop_id = $shop['id'];   
        }
        
        $from_price = $to_price = null;
        if (isset($price['from']) && $price['from']) {
            $from_price = $price['from'];    
        }
        if (isset($price['to']) && $price['to']) {
            $to_price = $price['to'];    
        }
        
        if ($seasons = Cache::instance()->get('category_seasons_'.$category->id.'_'.$brand_id.'_'.$color_id.'_'.$composition_id.'_'.$shop_id.'_'.$from_price.'_'.$to_price)) {
            return $seasons;
        }
        
        $stat = DB::select('season_id', array(DB::expr('COUNT( * )'), 'count'))
                    ->from('product_seasons')
                    ->join('products', 'LEFT')
                    ->on('products.id', '=', 'product_seasons.product_id')
                    ->where('category_id', 'IN', $ids)
                    ->where('available', '=', 1);
                    
        if ($brand_id) {
            $stat->where('brand_id', '=', $brand_id); 
        }   
             
        if ($color_id) {
            $stat->join('product_colors', 'LEFT')
                  ->on('product_colors.product_id', '=', 'products.id')
                  ->where('product_colors.color_id', '=', $color_id);   
        }
        
        if ($composition_id) {
            $stat->join('product_compositions', 'LEFT')
                  ->on('product_compositions.product_id', '=', 'products.id')
                  ->where('product_compositions.composition_id', '=', $composition_id);   
        }
        
        if ($shop_id) {
            $stat->where('shop_id', '=', $shop_id);    
        }
        
        if ($from_price) {
            $stat->where('price', '>=', $from_price);    
        }
        if ($to_price) {
            $stat->where('price', '<=', $to_price); 
        }
                    
        $stat = $stat
                    ->group_by('season_id')
                    ->order_by('count', 'DESC')
                    ->execute()
                    ->as_array();
        
        $seasons = array();
        $all_seasons = self::get_all_seasons();
        foreach ($stat as $season) {
            $seasons[$season['season_id']] = $all_seasons[$season['season_id']];
            $seasons[$season['season_id']]['count'] = $season['count'];            
        } 
        usort($seasons, array(__CLASS__, '_compare'));
        
        Cache::instance()->set('category_seasons_'.$category->id.'_'.$brand_id.'_'.$color_id.'_'.$composition_id.'_'.$shop_id.'_'.$from_price.'_'.$to_price, $seasons);
        return $seasons;     
    }
    
    public static function get_all_seasons()
    {
        $seasons = array();
        foreach (self::$seasons as $url => $value) {
            $seasons[$value] = self::$season_options[$value];
            $seasons[$value]['url'] = $url;
            $seasons[$value]['id']  = $value;        
        } 
        return $seasons;   
    }
    
    public static function get_season_by_url($url)
    {
        $seasons = self::get_all_seasons();
        foreach ($seasons as $season) {
            if ($season['url'] == $url) {
                return $season;    
            }    
        }
        return null;
    }
}